<?PHP  // $Id: view.php,v 1.3.2.2 2009/02/23 19:22:41 dlnsk Exp $

/// This page prints a particular instance of diemrenluyen

require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/locallib.php');


    $id   = optional_param('id', -1, PARAM_INT);   // Course Module ID, or
    $studentid			= optional_param('student', 0, PARAM_INT);
    $printing			= optional_param('printing');

    if ($id) {
        if (! $cm = get_record("course_modules", "id", $id)) {
            error("Course Module ID was incorrect");
        }
    
        if (! $course = get_record("course", "id", $cm->course)) {
            error("Course is misconfigured");
        }
    
        if (! $diemrenluyen = get_record("diemrenluyen", "id", $cm->instance)) {
            error("Course module is incorrect");
        }

    } else {
    	error("Module id is incorrect.");
    }

    require_login($course->id);

    if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
        print_error('badcontext');
    }
    
    if (!$studentid && (has_capability('mod/diemrenluyen:takemark', $context))) {
        redirect("manage.php?id=$cm->id");
    }
    if (!$studentid && has_capability('mod/diemrenluyen:viewreports', $context)) {
        redirect("report.php?id=$cm->id");
    }
    
    if (! $user = get_record('user', 'id', $USER->id) ) {
        error("No such user in this course");
    }
//    Can co quyen view de xem trang nay
    require_capability('mod/diemrenluyen:view', $context);
	$student = false;
    if ($studentid) {
    	if ($studentid == $USER->id or has_capability('mod/diemrenluyen:viewreports', $context)) {
		    if (!$student = get_record('user', 'id', $studentid) ) {
		        error("No such user in this course");
		    }
    	}
        if ($studentid != $USER->id && !has_capability('mod/diemrenluyen:viewreports', $context))
        {
            error(get_string("nothavepermission","diemrenluyen"));
        }
    } else
    {
        $studentid = $USER->id;
    }

	
    
    if ($student) {
            $user = $student;
    }
	


/// Print headers
	    $navlinks[] = array('name' => $diemrenluyen->name, 'link' => "view.php?id=$id", 'type' => 'activityinstance');
	    $navlinks[] = array('name' => get_string('baocaodiemrenluyen', 'diemrenluyen'), 'link' => null, 'type' => 'title');
	    $navigation = build_navigation($navlinks);
	    print_header("$course->shortname: ".$diemrenluyen->name.' - ' . fullname($student), $course->fullname,
	                 $navigation, "", "", true, "&nbsp;", navmenu($course));
	//add info to log
	add_to_log($course->id, 'diemrenluyen', 'student view', "mod/diemrenluyen/view.php?course=$course->id&amp;student=$USER->id", $USER->lastname.' '.$USER->firstname);

//Lay du lieu ve diem ren luyen cua sinh vien.
$marks = get_records("diemrenluyen_sinhvien", "userid", $studentid,"month ASC");
print_box_start();
//Duong dan den trang thong tin sinh vien.
echo "<center><a href=\"$CFG->wwwroot/user/view.php?id={$studentid}\">". fullname($student)."</a></center>";
if ($marks)
{
    unset($table);
    $table->head[] = get_string("month","diemrenluyen");
    $table->align[] = 'center';
    $table->size[] = '';
    $table->wrap[2] = 'nowrap';
    //Ten tieu de cua cot du lieu diem, khi di chuyen chuot len se duoc hien thi huong dan.
    for ($jj=1;$jj<=5;$jj++) {
        $table->head[] = "<a href=\"javascript:void(0);\"
            onmouseover=\"return overlib(' ". get_string("desc$jj","diemrenluyen") . "',
            AUTOSTATUS, WRAP);\" onmouseout=\"nd();\">". get_string("dieu$jj","diemrenluyen") ."</a>";
        $table->align[] = 'center';
        $table->size[] = '';
    }

    $table->head[] = get_string("total","diemrenluyen");
    $table->align[] = 'center';
    $table->size[] = '';
//    xep loai diem ren luyen sinh vien: gioi kha trung binh ...
    $table->head[] = get_string("type","diemrenluyen");
    $table->align[] = 'center';
    $table->size[] = '';

    $table->head[] = get_string("Marked by","diemrenluyen");
    $table->align[] = 'center';
    $table->size[] = '';
    $i=0;
    $total=0; //Tong cong tat ca diem ren luyen cua sinh vien;

    foreach ($marks as $mark)
    {
        //kiem tra xem diem ren luyen trong du lieu co dung cua khoa hoc hay khong.
        if ($mark->diemrenluyenid == $diemrenluyen->id)
        {
            $table->data[$i][] = date("m/Y", $mark->month) ;
            $total_mark=0; //Tong cong diem ren luyen moi thang;
            for ($jj=1;$jj<=5;$jj++) {
                $drl_mark = "d" . $jj;
                $total_mark += intval($mark->$drl_mark);
                $table->data[$i][] =$mark->$drl_mark;
               }
            $total+=$total_mark;
            $table->data[$i][] = $total_mark;
            $table->data[$i][] = xeploai($total_mark);
            $table->data[$i][] =  fullname(get_record('user', 'id', $mark->takenby));
            $i++;
        }
    }
//    In tong ket diem ren luyen cua sinh vien qua tat cac cac thang
    if ($i>0)
    {
        print_table($table);

        unset($table);
        $table->head[] = get_string("total","diemrenluyen");
        $table->align[] = 'center';
        $table->size[] = '';

        $table->head[] = get_string("trungbinh","diemrenluyen");
        $table->align[] = 'center';
        $table->size[] = '';

        $table->head[] = get_string("xeploai","diemrenluyen");
        $table->align[] = 'center';
        $table->size[] = '';
        $table->data[0][] = $total;
        $table->data[0][] = intval($total) / $i;
        $table->data[0][] = xeploai( $total / $i);

        print_table($table);

    } else 
    echo "<center>".get_string ("notyet","diemrenluyen"). "</center>"; //Khong co thong tin ve du lieu diem

} else {
    echo "<center>".get_string ("notyet","diemrenluyen"). "</center>"; //Khong co thong tin ve du lieu diem
}
print_box_end();
print_footer($course);

?>